package com.yx.pc.api.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.utils.R;
import com.yx.modules.api.dao.TbUserMapper;
import com.yx.modules.api.entity.TbUser;
import com.yx.pc.api.service.PcTdOrderService;
import com.yx.system.dao.*;
import com.yx.system.dto.BookStatisticsDto;
import com.yx.system.entity.*;
import com.yx.system.service.TdOrderService;
import com.yx.utils.*;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PcTdOrderServiceImpl implements PcTdOrderService {

    @Autowired
    private TdOrderMapper tdOrderMapper;

    @Autowired
    private TdSectionMapper tdSectionMapper;

    @Autowired
    private TdBookMapper tdBookMapper;

    @Autowired
    private TdOnlinePayLogMapper tdOnlinePayLogMapper;

    @Autowired
    private TdWalletPayLogMapper tdWalletPayLogMapper;

    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return tdOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TdOrder record) {
        return tdOrderMapper.insert(record);
    }

    @Override
    public int insertSelective(TdOrder record) {
        return tdOrderMapper.insertSelective(record);
    }

    @Override
    public TdOrder selectByPrimaryKey(Integer id) {
        return tdOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TdOrder record) {
        return tdOrderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TdOrder record) {
        return tdOrderMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<TdOrder> queryList(Map<String, Object> map) {
        return tdOrderMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return tdOrderMapper.queryTotal(map);
    }

    @Override
    public List<TdOrder> queryAl() {
        return tdOrderMapper.queryAll();
    }

    @Override
    public TdOrder queryByBillNo(String billNo) {
        return tdOrderMapper.queryByBillNo(billNo);
    }

    @Override
    public Page<TdOrder> queryPageList(Page<TdOrder> page, Map<String, Object> map) {
        page.setRecords(tdOrderMapper.queryPageList(page, map));
        return page;
    }

    @Override
    public Page<BookStatisticsDto> sameMonth(Page<BookStatisticsDto> page, Map<String, Object> map) {
        page.setRecords(tdOrderMapper.sameMonth(page, map));
        return page;
    }

    @Override
    public Page<BookStatisticsDto> sameMonth2(Page<BookStatisticsDto> page, Map<String, Object> map) {
        page.setRecords(tdOrderMapper.sameMonth2(page, map));
        return page;
    }

    @Override
    public Page<BookStatisticsDto> allStatistics(Page<BookStatisticsDto> page, Map<String, Object> map) {
        page.setRecords(tdOrderMapper.allStatistics(page, map));
        return page;
    }

    @Override
    public R create(JSONArray ja, TbUser tbUser) {
        for (int i = 0; i < ja.size(); i++) {
            TdSection tdSection = tdSectionMapper.selectByPrimaryKey(Integer.parseInt(ja.get(i).toString()));
            TdBook tdBook = tdBookMapper.selectByPrimaryKey(tdSection.getBookid());
            TdOrder order = new TdOrder();
            order.setUuid(tbUser.getUuid());
            //为订单赋值
            order.setBillno(OrderUtil.getOrderNo2());
            if (tdBook == null) {
                return R.error("未查询到书籍信息!");
            }
            if (tdBook.getSource()!=null){
                order.setSource(tdBook.getSource());
            }
            if (tdBook.getOrigin()!=null){
                order.setOrigin(tdBook.getOrigin());
            }
            Date date = new Date();
            order.setBookid(tdBook.getId());
            order.setCreatedate(date);
            order.setDay(DateUtil.getDay(date));
            order.setMonth(DateUtil.getMonth(date));
            order.setYear(DateUtil.getYear(date));
            order.setTotalfee(tdSection.getTotalfee());
            order.setUsername(tbUser.getUsername());
            order.setUserphone(tbUser.getMobile());
            order.setSectionid(tdSection.getId());
            order.setOrderstatus(PayStatus.success.getName());
            order.setIspay(true);
            order.setPaytype("out");
            order.setCreateuser(tbUser.getUsername());
            tdOrderMapper.insert(order);
            //存入支付数据
            TdOnlinePayLog opl = new TdOnlinePayLog();
            opl.setCreatedate(new Date());
            opl.setBillno(order.getBillno());
            opl.setCreateuser(tbUser.getUsername());
            opl.setUuid(UUIDUtil.getUUID());
            opl.setUserid(tbUser.getUserId() + "");
            opl.setPaytype(order.getPaytype());
            opl.setTitle(tdBook.getBookname());
            opl.setTotalfee(order.getTotalfee());
            opl.setPaystatus(PayStatus.success.getName());
            opl.setUserpaystatus(PayType.ALI_WEB.getName());
            opl.setWebhookstatus(PayStatus.success.getName());
            tdOnlinePayLogMapper.insert(opl);

            TdWalletPayLog twp = new TdWalletPayLog();
            twp.setBillno(order.getBillno());
            twp.setCreatedate(new Date());
            twp.setCreateuser(tbUser.getUsername());
            twp.setPaystatus(PayStatus.success.getName());
            twp.setReturntype(PayStatus.success.getName());
            twp.setTitle(tdSection.getSectionname());
            twp.setTotalfee(tdSection.getTotalfee());
            twp.setUserpaystatus(PayType.ALI_WEB.getName());
            twp.setPaytype(order.getPaytype());
            twp.setUuid(tbUser.getUuid());
            twp.setWebhookstatus(PayStatus.success.getName());
            tdWalletPayLogMapper.insert(twp);

            tbUser.setBalance(tbUser.getBalance() - tdSection.getTotalfee());
            if (tbUser.getBalance()<=0){
                return R.error("信息，异常请联系管理员");
            }
            tbUserMapper.updateByPrimaryKey(tbUser);
            tdBook.setSell(tdBook.getSell()+1);
            tdBook.setSubscibeMoney(tdBook.getSubscibeMoney()+tdSection.getTotalfee());
            /*
            * 比赛订阅金额增加
            */
            tdBook.setGameSubscribeCount(tdBook.getGameSubscribeCount()+tdSection.getTotalfee());
            tdBookMapper.updateByPrimaryKey(tdBook);
            /*//找到图书对应的作者
            if (!tdBook.getAuthorCode().equals("0")){
                //根据作者code找到用户
                TbUser author = tbUserService.findByAuthor(tdBook.getAuthorCode());
                if (author!=null){
                    //抽成后的作者实际获取的蝉币数量(四舍五入)
                    Integer actualMoney = Integer.parseInt(new java.text.DecimalFormat("0").format(tdSection.getTotalfee()*(1-tbRation.getRation())));
                    //增加作者账户的蝉币数量
                    author.setBalance(author.getBalance()+actualMoney);
                    //更新用户信息
                    tbUserService.update(author);
                }
            }*/
        }
        return R.ok();
    }

    @Override
    public R buySections(Integer bookid, List<TdSection> tdSections, TbUser tbUser) {
        TdBook tdBook = tdBookMapper.selectByPrimaryKey(bookid);
        for (TdSection tdSection:tdSections) {
            TdOrder order = new TdOrder();
            order.setUuid(tbUser.getUuid());
            //为订单赋值
            order.setBillno(OrderUtil.getOrderNo2());
            if (tdBook == null) {
                return R.error("未查询到书籍信息!");
            }
            if (tdBook.getSource()!=null){
                order.setSource(tdBook.getSource());
            }
            if (tdBook.getOrigin()!=null){
                order.setOrigin(tdBook.getOrigin());
            }
            Date date = new Date();
            order.setBookid(tdBook.getId());
            order.setCreatedate(date);
            order.setDay(DateUtil.getDay(date));
            order.setMonth(DateUtil.getMonth(date));
            order.setYear(DateUtil.getYear(date));
            order.setTotalfee(tdSection.getTotalfee());
            order.setUsername(tbUser.getUsername());
            order.setUserphone(tbUser.getMobile());
            order.setSectionid(tdSection.getId());
            order.setOrderstatus(PayStatus.success.getName());
            order.setIspay(true);
            order.setPaytype("out");
            order.setCreateuser(tbUser.getUsername());
            tdOrderMapper.insert(order);
            //存入支付数据
            TdOnlinePayLog opl = new TdOnlinePayLog();
            opl.setCreatedate(new Date());
            opl.setBillno(order.getBillno());
            opl.setCreateuser(tbUser.getUsername());
            opl.setUuid(UUIDUtil.getUUID());
            opl.setUserid(tbUser.getUserId() + "");
            opl.setPaytype(order.getPaytype());
            opl.setTitle(tdBook.getBookname());
            opl.setTotalfee(order.getTotalfee());
            opl.setPaystatus(PayStatus.success.getName());
            opl.setUserpaystatus(PayType.ALI_WEB.getName());
            opl.setWebhookstatus(PayStatus.success.getName());
            tdOnlinePayLogMapper.insert(opl);

            TdWalletPayLog twp = new TdWalletPayLog();
            twp.setBillno(order.getBillno());
            twp.setCreatedate(new Date());
            twp.setCreateuser(tbUser.getUsername());
            twp.setPaystatus(PayStatus.success.getName());
            twp.setReturntype(PayStatus.success.getName());
            twp.setTitle(tdSection.getSectionname());
            twp.setTotalfee(tdSection.getTotalfee());
            twp.setUserpaystatus(PayType.ALI_WEB.getName());
            twp.setPaytype(order.getPaytype());
            twp.setUuid(tbUser.getUuid());
            twp.setWebhookstatus(PayStatus.success.getName());
            tdWalletPayLogMapper.insert(twp);

            tbUser.setBalance(tbUser.getBalance() - tdSection.getTotalfee());
            if (tbUser.getBalance()<=0){
                return R.error("信息，异常请联系管理员");
            }
            tbUserMapper.updateByPrimaryKey(tbUser);
            tdBook.setSell(tdBook.getSell()+1);
            tdBook.setSubscibeMoney(tdBook.getSubscibeMoney()+tdSection.getTotalfee());
            /*
            * 比赛订阅金额增加
            */
            tdBook.setGameSubscribeCount(tdBook.getGameSubscribeCount()+tdSection.getTotalfee());
            tdBookMapper.updateByPrimaryKey(tdBook);
            /*//找到图书对应的作者
            if (!tdBook.getAuthorCode().equals("0")){
                //根据作者code找到用户
                TbUser author = tbUserService.findByAuthor(tdBook.getAuthorCode());
                if (author!=null){
                    //抽成后的作者实际获取的蝉币数量(四舍五入)
                    Integer actualMoney = Integer.parseInt(new java.text.DecimalFormat("0").format(tdSection.getTotalfee()*(1-tbRation.getRation())));
                    //增加作者账户的蝉币数量
                    author.setBalance(author.getBalance()+actualMoney);
                    //更新用户信息
                    tbUserService.update(author);
                }
            }*/
        }
        return R.ok();
    }


}
